//
//  InfoScrollBaseView.m
//  marrymarry
//
//  Created by lsj on 16/9/19.
//  Copyright © 2016年 cf. All rights reserved.
//

#import "InfoScrollBaseView.h"
@interface InfoScrollBaseView()<UIScrollViewDelegate>
{
    NSMutableArray * _lookArry;
    NSMutableArray * _natureArry;
    NSMutableArray * _interestArry;
    NSMutableArray * _usualArry;
    NSMutableArray * _addArry;
    NSMutableArray * _myArry;
    
    NSString * _lookStr;
    NSString * _natureStr;
    NSString * _interestStr;

    int  _dangerNum;
    int countNum;
    int hangNum;
    int LieNum;
    int midNum;
    int btnTag;
    BOOL isSelect;
}

@end

@implementation InfoScrollBaseView
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        _dangerNum = 0;
       
    }
    return self;
}


-(void)reloadTheBaseViewWithDic:(NSDictionary *)dic andFenKongArry:(NSMutableArray *)fkArry
{
    _dangerNum =(int)fkArry.count;
    _addArry = [NSMutableArray new];
    _myArry = [NSMutableArray new];
    self.layer.cornerRadius = 6;
    if (dic!=nil) {
        _lookArry = [dic objectForKey:LOOK_ARRY];
        _natureArry =[dic objectForKey:NATURE_ARRY];
        _interestArry = [dic objectForKey:INTERRST_ARRY];
    }
    self.myScrollViews.contentSize = CGSizeMake(self.frame.size.width*3,0);
    self.myScrollViews.delegate = self;
    self.myScrollViews.pagingEnabled = YES;
    self.myScrollViews.showsHorizontalScrollIndicator = NO;
    self.myScrollViews.showsVerticalScrollIndicator = NO;
    self.myScrollViews.bounces =NO;
   
    self.myScrollViews.tag =100;
    _usualArry = [NSMutableArray new];
    btnTag = 700;
    for (int i=0; i<3; i++)
    {
          countNum = 0;
        if (i == 0) {
            _usualArry = _lookArry;

        }else if (i==1)
        {
            _usualArry =_natureArry;
        }else if (i == 2)
        {
            _usualArry = _interestArry;
        }
        
        if (_usualArry.count%4 == 0) {
            LieNum =(int)_usualArry.count/4;
            midNum = 4;
        }else
        {
            LieNum = (int)_usualArry.count/4+1;
            midNum = _usualArry.count%4;
        }
            //创建9个和屏幕大小一样view
            UIView *v = [[UIView alloc]initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.myScrollViews.frame.size.height)];
        v.tag = 1000+i;
            v.backgroundColor = RGB(239, 239, 239);
        
        for (int j = 0; j<LieNum; j++) {
            if (j==LieNum-1) {
                hangNum =midNum;
            }else
            {
                hangNum =4;
            }
            for (int k = 0; k<hangNum; k++) {
                
                UIButton * biaoQianBtn =[[UIButton alloc]init];
                biaoQianBtn.frame = CGRectMake(8*(k+1)+((self.frame.size.width-40)/4)*k, 8*(j+1)+((self.myScrollViews.frame.size.height-40)/4)*j,(self.frame.size.width-40)/4, ((self.myScrollViews.frame.size.height-40)/4));
                biaoQianBtn.backgroundColor = [UIColor whiteColor];
                biaoQianBtn.layer.cornerRadius = 5;
                biaoQianBtn.tag =btnTag;
                [biaoQianBtn setTitle:_usualArry[countNum] forState:UIControlStateNormal];
                [biaoQianBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
                [biaoQianBtn addTarget:self action:@selector(biaoQianSelect:) forControlEvents:UIControlEventTouchUpInside];
                biaoQianBtn.titleLabel.font = [UIFont systemFontOfSize:14];
                [v addSubview:biaoQianBtn];
                countNum++;
                btnTag ++;
            }
        }
        
        
            [self.myScrollViews addSubview:v];
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self addGestureRecognizer:tap];
    [self.MypageCotrol addTarget:self action:@selector(imgChange:) forControlEvents:UIControlEventValueChanged];
    self.MypageCotrol.tag = 101;
}

-(void)biaoQianSelect:(UIButton *)btn
{

    if (btn.selected) {
        btn.selected = NO;
        btn.backgroundColor = [UIColor whiteColor];
        _dangerNum -- ;
    }else{
        if (_dangerNum>=16) {
            [SVProgressHUD showInfoWithStatus:@"最多选择16个标签"];
        }else{
        btn.selected = YES;
        btn.backgroundColor = [UIColor orangeColor];
            _dangerNum ++;
            NSLog(@"_dangerNum ===== %d",_dangerNum);
        }
    }
    
}
- (IBAction)saveTheBQ:(UIButton *)sender {
    for (int i = 0; i<3; i++) {
        [_myArry removeAllObjects];
        UIView * view = [self viewWithTag:i+1000];
        for (UIButton * btn in view.subviews) {
            if ([btn isKindOfClass:[UIButton class]]) {
                if (btn.selected) {
                [_addArry addObject:btn.titleLabel.text];
                [_myArry addObject:btn.titleLabel.text];
                }
            }
        
        }
        if (i == 0) {
            _lookStr = [_myArry componentsJoinedByString:@","];
        }else if (i==1)
        {
            _natureStr = [_myArry componentsJoinedByString:@","];
        }else if (i ==2)
        {
            _interestStr = [_myArry componentsJoinedByString:@","];
        }
    }
    NSMutableDictionary * addDic = [NSMutableDictionary new];
    [addDic setObject:_addArry forKey:@"addArr"];
    [Tools syncNSUserDeafaultsByKey:@"addarr" withValue:addDic];
      self.saveBlock(addDic,_lookStr,_natureStr,_interestStr);
    [self removeFromSuperview];
    
}

-(void)getSaveDicWithBlock:(saveBtnBlock)svBlock
{
    self.saveBlock = svBlock;
}


- (void)imgChange:(UIPageControl *)page
{
    //点击pagecontrol,scrollview跟随滑动
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:100];
    //设置scroll的偏移量
    //当前页*图片宽度
    switch (self.MypageCotrol.currentPage) {
        case 0:
            self.titleLabel.text = @"外形";
            break;
        case 1:
            self.titleLabel.text = @"性格";
            break;
        case 2:
            self.titleLabel.text = @"兴趣爱好";
            break;
        default:
            break;
    }
    [scroll setContentOffset:CGPointMake(page.currentPage * scroll.frame.size.width, 0) animated:YES];
}



- (void)tap
{
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:100];
    //带动画的移动
    [scroll setContentOffset:CGPointMake(self.frame.size.width,0) animated:YES];
}
#pragma mark -UIScrollViewDelegate
//scrollView正在滚动.任何scrollView偏移量的修改,都会触发此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //这里会不断的调用,不适合做循环等耗时操作
    //    NSLog(@"视图正在滚动");
    //    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"视图即将开始拖拽");
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"视图即将停止拖拽");
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"视图已经停止拖拽");
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"视图即将开始减速");
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"视图已经减速");
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    
    
    //停止加速的代理里面,来更改
    
    //取到当前图片的位置
    int index = scrollView.contentOffset.x/self.frame.size.width;
    NSLog(@"index==%d",index);
    
    //将位置设置给UIPageControl,来实现联动
    UIPageControl *page =(UIPageControl *) [self viewWithTag:101];
    //设置当前页码
    page.currentPage = index;
    switch (self.MypageCotrol.currentPage) {
        case 0:
            self.titleLabel.text = @"外形";
            break;
        case 1:
            self.titleLabel.text = @"性格";
            break;
        case 2:
            self.titleLabel.text = @"兴趣爱好";
            break;
        default:
            break;
    }

    
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    //ios为我们scrollView提供一个功能.点击状态回到顶部
    //返回 NO,则关闭点击状态返回
    return YES;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"已经返回到顶部");
}
-(void)readTheToolsToLoadViewsWith:(NSMutableArray *)toolArry
{
    for (int i = 0; i<3; i++) {
        UIView * view = [self viewWithTag:i+1000];
        for (UIButton * btn in view.subviews) {
            if ([btn isKindOfClass:[UIButton class]]) {
                if ( [toolArry indexOfObject:btn.titleLabel.text] != NSNotFound) {
                    btn.backgroundColor = [UIColor orangeColor];
                    NSLog(@"%@",btn.titleLabel.text);
                    NSLog(@"%@",toolArry);
                    btn.selected =YES;
                }
            }
        }
        
    }

}


@end
